package me.pplay.playtalk.activity.friends;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class SearchByIDAcitivity extends BaseActivity {
	private EditText searchbyid_username, searchbyid_alias, searchbyid_email, searchbyid_phnum;
	private Button searchbyid_btn;
	private ProgressDialog progressDialog;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			LogManager.LogShow("SearchByIDAcitivity", "handleMessage msg.what = " + msg.what);
			switch (msg.what) {
			case TpChat.CHAT_GET_SEARCHBUDDIES_OK:
				progressDialog.dismiss();
				Intent intent = new Intent(SearchByIDAcitivity.this,
						SearchResultActivity.class);
				startActivity(intent);
				break;
			case TpChat.CHAT_GET_SEARCHBUDDIES_ERR: {
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Unkown err" : rspdata.getString());
			}
				break;
			case TpChat.CHAT_GET_SEARCHBUDDIES_TIMEOUT:
				progressDialog.dismiss();
				showToast("Time out");
				break;
			default:
				break;
			}
		}

	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_searchbyid);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		searchbyid_username = (EditText) findViewById(R.id.searchbyid_username);
		searchbyid_alias = (EditText) findViewById(R.id.searchbyid_alias);
		searchbyid_email = (EditText) findViewById(R.id.searchbyid_email);
		searchbyid_phnum = (EditText) findViewById(R.id.searchbyid_phnum);
		searchbyid_btn = (Button) findViewById(R.id.searchbyid_btn);
		clickEvents();
	}

	private void clickEvents() {
		searchbyid_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String username = searchbyid_username.getText().toString().trim();
				String alias = searchbyid_alias.getText().toString().trim();
				String email = searchbyid_email.getText().toString().trim();
				String phnum = searchbyid_phnum.getText().toString().trim();
				getFriendsData("fuzzy=" + username 
						+ "&" + "nickName=" + alias 
						+ "&" + "email=" + email 
						+ "&" + "mobile=" + phnum
						+ "&" + "page=1&pageSize=40");
			}
		});
	}

	private void getFriendsData(String condition) {
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		TpChat tpChat = appState.getTpChat();
		progressDialog = ProgressDialog.show(SearchByIDAcitivity.this, getString(R.string.loading), getString(R.string.waittng), true, false); 
		progressDialog.show();
		tpChat.tp_chat_search_friends(condition,new DataCallBack() {
			
			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				Message msg = new Message(); 
				msg.what = result; 
				msg.obj = resultdata;
				if (TpChat.CHAT_GET_SEARCHBUDDIES_OK == result) {
					msg.obj = obj;
				}else if (TpChat.CHAT_GET_SEARCHBUDDIES_ERR == result) {
					
				}else if (TpChat.CHAT_GET_SEARCHBUDDIES_TIMEOUT == result) {
					
				}
				handler.sendMessage(msg);
			}
		});
	}
}
